package Veterinaria.servlets;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Veterinaria.excepcion.DAOExcepcion;
import Veterinaria.modelo.DetalleArticuloCita;
import Veterinaria.modelo.FacturaDetalleTMP;
import Veterinaria.negocio.GestionCita;
import Veterinaria.negocio.GestionFacturaDetalle;

/**
 * Servlet implementation class FacturaTempServlet
 */
@WebServlet("/FacturaTempServlet")
public class FacturaTempServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public FacturaTempServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		GestionFacturaDetalle facdet = new GestionFacturaDetalle();

		String idfactura = request.getParameter("idfactura");
		String idNomCliente = request.getParameter("idNomCliente");
		String NomCliente = request.getParameter("NomCliente");
		String fechafactura = request.getParameter("fechafactura");

		String idarticulo = request.getParameter("idarticulo");
		String txtarticulo = request.getParameter("txtarticulo");
		String txtprecio = request.getParameter("txtprecio");
		String numcantidad = request.getParameter("numcantidad");
		String citas = request.getParameter("txtcitas");
		
		

		System.out.println(citas);
		if (idarticulo!=""){
			// Insertar  Articulos a factura
			double xprecio = Double.parseDouble(txtprecio);
			int xcantidad = Integer.parseInt(numcantidad);
			double importe = xprecio * xcantidad;
			double total = 0;			
				if (idfactura!=""){
					try {
						facdet.insertarfacturadetalletmp(idfactura, idarticulo,txtarticulo, xcantidad, importe);
					} catch (DAOExcepcion e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				try {
					GestionFacturaDetalle fact = new GestionFacturaDetalle();
					request.setAttribute("facturas", fact.listartemporal());
					Collection<FacturaDetalleTMP> o = null;
					try {
						o = fact.listartemporal();
					} catch (DAOExcepcion e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					if (o != null) {
						for (FacturaDetalleTMP x : o) {
							total = total + (x.getTotal());
						}
					}
					String simporte = String.valueOf(total);
					request.setAttribute("var5", simporte);
				} catch (Exception e) {
					System.out.println(e.getMessage());
				}
		}else if (citas!=""){
			// Insertar  Citas a Factura
//			GestionFacturaDetalle fact = new GestionFacturaDetalle();
		
			double importe=0;
			
			GestionCita cita = new GestionCita();
					
						try {
							request.setAttribute("facturas", cita.listarDetalleArticuloCita() );
	
						} catch (DAOExcepcion e2) {
							// TODO Auto-generated catch block
							e2.printStackTrace();
						}
	
					Collection<DetalleArticuloCita> o =null;
					try {
						o = cita.listarDetalleArticuloCita();
		
					} catch (DAOExcepcion e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
			if(o!= null) { 
				for(DetalleArticuloCita x : o) {
					try {
						double monto = x.getOarticulo().getPrecio() * x.getCantidad();
						facdet.insertarfacturadetalletmp(idfactura, x.getOarticulo().getCod_articulo(),x.getOarticulo().getDescripcion(), x.getCantidad(),monto);
						importe = importe + (monto);
						String simporte = String.valueOf(importe);
						request.setAttribute("var5", simporte);	
					} catch (DAOExcepcion e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					GestionFacturaDetalle fact = new GestionFacturaDetalle();
					try {
						request.setAttribute("facturas", fact.listartemporal());
					} catch (DAOExcepcion e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			//
			}

		}else if (citas=="" || idarticulo==""){
			
		}

		request.setAttribute("var1", idfactura);
		request.setAttribute("var2", idNomCliente);
		request.setAttribute("var3", NomCliente);
		request.setAttribute("var4", fechafactura);
		
		RequestDispatcher rd = request.getRequestDispatcher("factura.jsp");
		rd.forward(request, response);		
		
	}

}
